﻿using System;
using System.Web;
using System.Text;
using System.Data ;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using System.Reflection;
using Microsoft.ApplicationBlocks.Data;
using AjaxPro;
namespace AnyOffice.general
{
    public partial class topbar : Page
    {
        public string theme = "2009/8";
        public string theme2 = "2009";
        public string  menuId ="01";// string.Empty ;
        public bool menuHide;
        public string userName = string.Empty;
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
     
     
        public DataTable dtbMenu= new DataTable();
        public DataTable dtbTheme = new DataTable();
        public string onlineFile = "user_online.aspx";
        public int ajaxInternal;
        public string callsound = string.Empty;
        public string smson = string.Empty;
        public string textState = string.Empty;
        public int state = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            Utility.RegisterTypeForAjax(typeof(AnyOffice.general.status_bar.AjaxMethods));
            Utility.RegisterTypeForAjax(typeof(AnyOffice.general.ipanel.AjaxMethods));
            if (!this.IsPostBack)
            {
                ConfigManager manager = new ConfigManager();
                UserCache cache = new UserCache(base.User.Identity.Name);


                if (string.IsNullOrEmpty(Request.UrlReferrer.Query) || (Request.UrlReferrer.Query.IndexOf("?theme=") == -1))
                {
                    theme = theme2 + "/" + cache.Theme.ToString();
                }
                else
                {
                    string themeid = Request.UrlReferrer.Query.Replace("?theme=", "");
                    theme = theme2 + "/" + themeid;
                    UsersDao dao2 = new UsersDao();
                    UsersData data = new UsersData();
                    data.UserId = base.User.Identity.Name;
                    data.Theme = Convert.ToInt32(themeid);
                    dao2.UpdatePersonality(new SqlConnection(ConfigManager.ConnectionString), data);
                    UserCache.Update(base.User.Identity.Name);
                }
                try
                {
                    this.userName = new UserCache().UserName;
                    this.litBannerText.Text = InterfaceCache.BannerText;
                    if (InterfaceCache.LogoImg != string.Empty)
                    {
                        this.litLogoImg.Text = string.Concat(new object[] { "<td width='", InterfaceCache.LogoWidth, "' align='center'><img src='/images/interface/", InterfaceCache.LogoImg, "' width='", InterfaceCache.LogoWidth, "' height='", InterfaceCache.LogoHeight, "'>&nbsp;&nbsp;</td>" });
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }

                try
                {

                    menuHide = cache.MenuHide;
                    string funcIdStr = cache.FuncIdStr;
                    string commandText = "SELECT * FROM [dbo].[Sys_Menu] WHERE [MenuId] IN(SELECT DISTINCT left([MenuId],2) FROM [dbo].[Sys_Function] WHERE CHARINDEX(','+ CAST([FuncId] as nvarchar(20)) + ',','" + funcIdStr + "')>0)";
                    this.dtbMenu = SqlHelper.ExecuteDataset(new SqlConnection(ConfigManager.ConnectionString), CommandType.Text, commandText).Tables[0];
                    DataSet set = new DataSet();
                     
                    set.ReadXml(Server.MapPath("/theme2009/theme.xml"));
                    dtbTheme = set.Tables[0];
                    this.ajaxInternal = manager.AjaxInterval;
                    this.smson = cache.SmsOn.ToString().ToLower();
                    this.callsound = cache.CallSound.ToString();
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }

                //this.lblUserName.Text = cache.UserName;
                // this.lblDeptName.Text = cache.DeptName;
                // this.lblPrivName.Text = cache.PrivName;
                this.imgAvatar.ImageUrl = "/images/avatar/" + cache.Avatar.ToString() + ".gif";
                this.state = cache.State;
                switch (cache.State)
                {
                    case 1:
                        this.textState = "联机";
                        break;

                    case 2:
                        this.textState = "忙碌";
                        break;

                    case 3:
                        this.textState = "离开";
                        break;

                    default:
                        this.textState = "联机";
                        break;
                }
            }
        }
    }
}
